package cn.lele.reggie.controller;

import cn.lele.reggie.common.BaseContext;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import cn.lele.reggie.common.R;
import cn.lele.reggie.entity.Employee;
import cn.lele.reggie.service.EmployeeService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private HttpServletRequest request;

    /**
     * 新增员工
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> insert(@RequestBody Employee employee){
        employee.setPassword("123456");
        employee.setIsAdmin(0);
        employeeService.save(employee);
        return R.success("添加成功");
    }

    /**
     * 根据id查员工
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R<Employee> selectById(@PathVariable Long id){
        LambdaUpdateWrapper<Employee> employeeLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        employeeLambdaUpdateWrapper.eq(Employee::getId,id).set(Employee::getUpdateUser, BaseContext.getCurrentId());
        employeeService.update(employeeLambdaUpdateWrapper);
        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }

    /**
     * 更新员工信息
     * @param employee
     * @return
     */
    @PutMapping
    public R<Employee> update(@RequestBody Employee employee){
        employeeService.updateById(employee);
        return R.success(employee);
    }

    /**
     * 分页查询+条件查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize,String name){
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name!=null,Employee::getName,name)
                .orderByDesc(Employee::getUpdateTime)
                .ne(Employee::getId,1);
        Page employeePage = new Page(page,pageSize);
        employeeService.page(employeePage,wrapper);
        return R.success(employeePage);
    }

    /**
     * 登录
     * @param emp
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee emp){
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        String password = emp.getPassword();
        wrapper.eq(Employee::getUsername,emp.getUsername()).eq(Employee::getPassword,password);
        Employee one = employeeService.getOne(wrapper);
        if(one!=null&&one.getStatus() == 0){
            return R.error("账号已禁用");
        }
        if(one!=null){
            request.getSession().setAttribute("empId",one.getId());
            return R.success(one);
        }else {
            return R.error("用户名或密码错误");
        }
    }

    /***
     * 退出登录
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(){
        request.getSession().removeAttribute("empId");
        log.info("已移除session中employee");
        return R.success("退出成功");
    }

}
